package com.java.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.HospitalFeignClient;
import com.java.common.helper.HttpRequestHelper;
import com.java.config.exceptioin.YyghException;
import com.java.mapper.PayMentMapper;
import com.java.model.order.*;
import com.java.model.vo.SignInfoVo;
import com.java.service.OrderService;
import com.java.service.PayMentInfoService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class PayMentInfoServiceImpl extends ServiceImpl<PayMentMapper, PaymentInfo> implements PayMentInfoService {

    @Autowired
    private PayMentMapper payMentMapper;

    @Autowired
    private OrderService orderService;

    @Autowired
    private HospitalFeignClient hospitalFeignClient;

    @Override
    public void paySuccess(String outTradeNo,Map<String, String> paramMap) {
        //1、根据订单编号得到支付记录
        //接受到outTradeNo 订单编号 和查询微信支付接口返回的结果
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no",outTradeNo);
        wrapper.eq("payment_type", PaymentTypeEnum.WEXIIN.getStatus());
        PaymentInfo paymentInfo = payMentMapper.selectOne(wrapper);
        //2、更新支付记录信息
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setTradeNo(paramMap.get("transaction_id"));
        paymentInfo.setCallbackContent(paramMap.toString());
        payMentMapper.updateById(paymentInfo);
        //3、根据订单号得到订单信息
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        //4、更新订单信息
        orderService.updateById(orderInfo);
        //5、调用医院接口 调用订单支付信息
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sing",sign);
        JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");
    }

    //保存交易订单
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //1、在保存交易订单之前现查看数据库中是否已经存在这个订单
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderInfo.getId());
        wrapper.eq("payment_type",paymentType);
        Integer count = payMentMapper.selectCount(wrapper);
        if(count>0) return;
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        //设置支付状态
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()
                +"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        payMentMapper.insert(paymentInfo);
    }
}
